package com.atlin.project.service.inner;

import com.atlin.beaconsapicommon.model.domain.interfaceInfo.UserInterfaceInfo;
import com.atlin.beaconsapicommon.service.InnerUserInterfaceInfoService;
import com.atlin.project.model.enums.UserInterfaceInfoStatusEnum;
import com.atlin.project.service.UserInterfaceInfoService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.dubbo.config.annotation.DubboService;

import javax.annotation.Resource;
import java.util.Objects;

/**
 * @author atlin
 * @description 用户接口调用服务 供内部系统使用
 * @createDate 2024/12/22 13:36
 */
@DubboService
public class InnerUserInterfaceInfoServiceImpl implements InnerUserInterfaceInfoService {

    @Resource
    private UserInterfaceInfoService userInterfaceInfoService;

    @Override
    public boolean invokePreCheck(long interfaceInfoId, long userId) {
        if (interfaceInfoId <= 0 || userId <= 0) {
            return false;
        }
        QueryWrapper<UserInterfaceInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("interfaceInfoId", interfaceInfoId);
        wrapper.eq("userId", userId);
        UserInterfaceInfo userInterfaceInfo = userInterfaceInfoService.getOne(wrapper);
        if (userInterfaceInfo == null) return false;

        // 检查是否禁用
        if (!Objects.equals(UserInterfaceInfoStatusEnum.NORMAL.getValue(), userInterfaceInfo.getStatus())) {
            return false;
        }

        return userInterfaceInfo.getLeftNum() > 0;
    }

    @Override
    public boolean invokeCount(long interfaceInfoId, long userId) {
        return userInterfaceInfoService.invokeCount(interfaceInfoId, userId);
    }
}
